package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseAccessoryConfigBean;
import com.koron.css2.baseConfig.bean.PubAccessoryTypeBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryCofingMapper;
import com.koron.css2.baseConfig.mapper.PubAccessoryTypeMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 获取允许上传的单据附件类型
 * @author bin
 *
 */
public class ReceiptAccessoryTypeList implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(ReceiptAccessoryTypeList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			String receiptType = JsonUtils.objectToPojo(req.getData(), String.class);
			BaseAccessoryCofingMapper mapper = factory.getMapper(BaseAccessoryCofingMapper.class);
			PubAccessoryTypeMapper pubAccessoryTypeMapper = factory.getMapper(PubAccessoryTypeMapper.class);
			List<BaseAccessoryConfigBean> accessoryConfigs = mapper.selectByReceiptType(receiptType);
			for(BaseAccessoryConfigBean accessoryConfig : accessoryConfigs) {
				PubAccessoryTypeBean accessoryType = pubAccessoryTypeMapper.selectByAccessNo(accessoryConfig.getAccessoryNo());
				accessoryConfig.setAccessoryType(accessoryType.getAccessoryName());
			}
			info.setData(accessoryConfigs);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			log.error(e.getMessage(), e);
		}
		return info;
	}

}
